﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using intelliEssay.Core;
using intelliEssay.Core.DocumentStructure;
using intelliEssay.Interop;
using System.Collections;

namespace intelliEssay.Checkers.AbstractChecker
{
   public class AbstractChecker:IEssayChecker
    {
        public void Run(Task task)
        {
            //throw new NotImplementedException();
            CheckAbstract(task);
        }

        public void Cancel(Task task)
        {
            throw new NotImplementedException();
        }

        public string GetDescription(string cultureInfo)
        {
            //throw new NotImplementedException();
            return "Checks Abstract ";
        }

        public string GetName(string cultureInfo)
        {
            //throw new NotImplementedException();
            return "Abstract Checker";
        }
        public void CommonAbstractcheck(Run run, RunInfoEx standardRuninfo, Task task)
        {
            // RunInfoEx standardRuninfo = new RunInfoEx(task.taskSetting.standardDocument, run, HostApplications.MsOffice2010);
            RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run, HostApplications.MsOffice2010);
            if (targetRuninfo["isbold"] != standardRuninfo["isbold"])
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否加粗出现错误", run, task, true));
            }
            if (targetRuninfo["isunderline"] != standardRuninfo["isunderline"])
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处下划线错误", run, task, true));
            }
            if (targetRuninfo["isitalic"] != standardRuninfo["isitalic"])
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否倾斜有错误", run, task, true));
            }
            if (standardRuninfo["color"] != targetRuninfo["color"] && targetRuninfo["color"] != "auto")
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的颜色不正确", run, task, true));
            }
            if (standardRuninfo["runfonts"] != targetRuninfo["runfonts"] && targetRuninfo["runfonts"] != "Calibri" && targetRuninfo["runfonts"] != "Times New Roman")
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的格式不正确", run, task, true));
            }
            if (standardRuninfo["fontsize"] != targetRuninfo["fontsize"])
            {               
               task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的大小不正确", run, task, true));
            }
        }
        public void CheckAbstract(Task task)
        {
            Paragraph standardAbstractCN = Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(DocumentParts.abstractCN, 0, task);
            Paragraph standardAbstractcontentCN = Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(DocumentParts.abstractCN_Content, 0, task);
            Paragraph standardAbstractEN = Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(DocumentParts.abstractEN, 0, task);
            Paragraph standardAbstractcontentEN = Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(DocumentParts.abstractEN_Content, 0, task);
            Paragraph targetAbstractCN = task.documentStructure.ChineseAbstract;
            List<Paragraph> targetAbstractcontentCN = task.documentStructure.ChineseAbstractContents;
            Paragraph targetAbstractEN = task.documentStructure.EnglishAbstract;
            List<Paragraph> targetAbstractcontentEN = task.documentStructure.EnglishAbstractContents;
            RunInfoEx standardAbstractCNruninfo = new RunInfoEx(task.taskSetting.standardDocument, standardAbstractCN.Elements<Run>().First(), HostApplications.MsOffice2010);
            RunInfoEx standardAbstractcontentCNruninfo = new RunInfoEx(task.taskSetting.standardDocument, standardAbstractcontentCN.Elements<Run>().First(), HostApplications.MsOffice2010);
            RunInfoEx standardAbstractENruninfo = new RunInfoEx(task.taskSetting.standardDocument, standardAbstractEN.Elements<Run>().First(), HostApplications.MsOffice2010);
            RunInfoEx standardAbstractcontentENruninfo = new RunInfoEx(task.taskSetting.standardDocument, standardAbstractcontentEN.Elements<Run>().First(), HostApplications.MsOffice2010);
            if (targetAbstractCN == null)
            {
               // task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Element, "没有找到摘  要两个字", targetAbstractCN, task, true));
                return;
            }
            else
            {
                if (targetAbstractCN.InnerText != standardAbstractCN.InnerText)
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Element, "摘  要两字的内容不正确", targetAbstractCN, task, true));
                }
            }
            if (targetAbstractEN == null)
            {
               // task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Element, "没有找到Abstract", targetAbstractCN, task, true));
                return;
            }
            else
            {
                if (targetAbstractEN.InnerText != standardAbstractEN.InnerText)
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Element, "此处Abstract的格式不正确", targetAbstractEN, task, true));
                }
            }
            foreach (Run run in targetAbstractCN.Elements<Run>())
            {
                if (run.InnerText.Replace(" ", "") != "")
                { CommonAbstractcheck(run, standardAbstractCNruninfo, task); }
                else
                { continue; }
            }
            foreach (Run run in targetAbstractEN.Elements<Run>())
            {
                if (run.InnerText.Replace(" ", "") != "")
                {
                   // CommonAbstractcheck(run, standardAbstractENruninfo, task);
                    RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run, HostApplications.MsOffice2010);
                    if (targetRuninfo["isbold"] != standardAbstractENruninfo["isbold"])
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否加粗出现错误", run, task, true));
                    }
                    if (targetRuninfo["isunderline"] != standardAbstractENruninfo["isunderline"])
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处下划线错误", run, task, true));
                    }
                    if (targetRuninfo["isitalic"] != standardAbstractENruninfo["isitalic"])
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否倾斜有错误", run, task, true));
                    }
                    if (standardAbstractENruninfo["color"] != targetRuninfo["color"] && targetRuninfo["color"] != "auto")
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的颜色不正确", run, task, true));
                    }
                    //if (standardAbstractcontentENruninfo["runfonts"] != targetRuninfo["runfonts"])
                    //{
                    //    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的格式不正确", run, task, true));
                    //}
                    if (standardAbstractENruninfo["fontsize"] != targetRuninfo["fontsize"])
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的大小不正确", run, task, true));
                    }
                }
                else
                { continue; }
            }
            foreach (Paragraph paragraph in targetAbstractcontentCN)
            {
                foreach (Run run in paragraph.Elements<Run>())
                {
                    if (run.InnerText.Replace(" ", "") != "")
                    {
                        CommonAbstractcheck(run, standardAbstractcontentCNruninfo, task);

                    }
                    else
                    { continue; }
                }
            }
            foreach (Paragraph paragraph in targetAbstractcontentEN)
            {
                foreach (Run run in paragraph.Elements<Run>())
                {
                    if (run.InnerText.Replace(" ", "") != "")
                    {
                       // CommonAbstractcheck(run, standardAbstractcontentENruninfo, task);
                        RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run, HostApplications.MsOffice2010);
                        if (targetRuninfo["isbold"] != standardAbstractcontentENruninfo["isbold"])
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否加粗出现错误", run, task, true));
                        }
                        if (targetRuninfo["isunderline"] != standardAbstractcontentENruninfo["isunderline"])
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处下划线错误", run, task, true));
                        }
                        if (targetRuninfo["isitalic"] != standardAbstractcontentENruninfo["isitalic"])
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否倾斜有错误", run, task, true));
                        }
                        if (standardAbstractcontentENruninfo["color"] != targetRuninfo["color"] && targetRuninfo["color"] != "auto")
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的颜色不正确", run, task, true));
                        }
                        //if (standardAbstractcontentENruninfo["runfonts"] != targetRuninfo["runfonts"])
                        //{
                        //    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的格式不正确", run, task, true));
                        //}
                        if (standardAbstractcontentENruninfo["fontsize"] != targetRuninfo["fontsize"] )
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的大小不正确", run, task, true));
                        }
                    }
                    else
                    { continue; }
                }
            }
        }
    }
}
